﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DictManage.Code;

namespace DictManage
{
    public partial class FrmManageDict : Form
    {
        public FrmManageDict(string dicttypeId,string dicttypeName)
        {
            InitializeComponent();
            this.txt_DictTypeID.Text = dicttypeId;
            this.txt_DictTypeName.Text = dicttypeName;
            this.txt_DictTypeName.Enabled = false;
            this.label6.Visible = false;
        }
        public FrmManageDict(string dicttypeId, string dicttypeName,string Id)
        {
            InitializeComponent();

            this.txt_DictTypeID.Text = dicttypeId;
            this.txt_DictTypeName.Text = dicttypeName;
            this.txt_DictTypeName.Enabled = false;
            this.label6.Text = Id;
            this.label6.Visible = false;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {//保存
            if (CheckInput())
            {
                string m_Id = Guid.NewGuid().ToString();
                string m_DtId = this.txt_DictTypeID.Text;
                string m_Name = this.txt_Name.Text;
                string m_Value = this.txt_Value.Text;
                string m_Seq = this.txt_Seq.Text;
                string m_Remark = this.txt_Remark.Text;
                if (this.Tag.ToString() == "添加")
                {
                    string sql = @"INSERT INTO [TB_DictData]([ID],[DictType_ID],[Name],[Value],[Remark],[Seq],[Editor],[LastUpdated])
                                            VALUES ('" + m_Id + "','" + m_DtId + "','" + m_Name + "','" + m_Value + "','" + m_Remark + "','" + m_Seq + "','','" + DateTime.Now.ToString() + "')";
                    OleDbAccess.ExecuteNonQury(sql);
                    MessageBox.Show("添加成功！", "系统提示");
                }
                else if (this.Tag.ToString() == "编辑")
                {
                    string sql_upp = @"UPDATE [TB_DictData] SET [Name] = '" + m_Name + "',[Value] = '" + m_Value + "' ,[Remark] = '" + m_Remark + "',[Seq] = '" + m_Seq + "' WHERE id='" + this.label6.Text + "'";
                    OleDbAccess.ExecuteNonQury(sql_upp);
                    MessageBox.Show("修改成功！", "系统提示");
                }
            }
        }
        private bool CheckInput()
        {
            bool flg = true;
            StringBuilder list=new StringBuilder();
            if (this.txt_DictTypeID.Text.Length == 0&&this.txt_DictTypeName.Text.Length==0)
            {
                list.AppendLine(" ● 字典大类不能为空！");
                flg = true;
            }
            else if (this.txt_Name.Text.Length == 0)
            {
                list.AppendLine(" ● 项目名称不能为空！");
                flg = true;
            }
            else if (this.txt_Value.Text.Length == 0)
            {
                list.AppendLine(" ● 项目值不能为空！");
                flg = true;
            }
            else if (this.txt_Seq.Text.Length == 0)
            {
                this.txt_Seq.Text = "0";
            }
            if (list.Length > 0)
            {
                flg = false;
                MessageBox.Show(list.ToString(), "系统提示");
            }
            return flg;
        }

        private void FrmManageDict_Load(object sender, EventArgs e)
        {
            if (this.Tag.ToString() == "编辑")
            {
                string sql = "select * from TB_DictData where id='" + this.label6.Text + "'";
                DataTable table = OleDbAccess.GetDataSet(sql).Tables[0];
                if (table != null && table.Rows.Count > 0)
                {
                    this.txt_Name.Text = table.Rows[0]["Name"].ToString();
                    this.txt_Value.Text = table.Rows[0]["Value"].ToString();
                    this.txt_Seq.Text = table.Rows[0]["Seq"].ToString();
                    this.txt_Remark.Text = table.Rows[0]["Remark"].ToString();
                    this.txt_DictTypeID.Text = table.Rows[0]["DictType_ID"].ToString();
                }
            }
        }
    }
}
